#!/bin/bash

# 定义要测试的 NuGet 源
sources=(
    # nuget
    "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json"
    "https://mirrors.huaweicloud.com/repository/nuget/v3/index.json"
    
    # debian
    "https://mirrors.ustc.edu.cn/debian/"           # 中科大 Debian 源
    "https://mirrors.tuna.tsinghua.edu.cn/debian/"  # 清华大学 Debian 源
    
    # android sdk
    "https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip"
    "https://mirrors.tuna.tsinghua.edu.cn/android/repository/commandlinetools-linux-10406996_latest.zip"
    "https://mirrors.ustc.edu.cn/android/repository/commandlinetools-linux-10406996_latest.zip"
    "https://developer.android.google.cn/studio/archive"
    "https://dl.google.cn/android/repository/commandlinetools-linux-10406996_latest.zip"
    "https://android-sdk.oss-cn-hangzhou.aliyuncs.com/commandlinetools-linux-10406996_latest.zip"
    "https://repo.huaweicloud.com/repository/android/repository/commandlinetools-linux-10406996_latest.zip"
    "https://mirrors.cloud.tencent.com/android/repository/commandlinetools-linux-10406996_latest.zip"
)

# 测试函数保持不变
test_nuget_source() {
    local url=$1
    echo "测试源: $url"
    
    # 使用 curl 代替 wget
    local curl_output=$(curl -sS -o /dev/null -w "%{http_code}" "$url")
    local curl_result=$?
    
    # 使用 dotnet nuget 测试源
    local nuget_output=$(dotnet nuget add source "$url" -n "test_source_$RANDOM" 2>&1)
    local nuget_result=$?
    
    # 详细输出
    echo "curl 返回码: $curl_result"
    echo "curl HTTP状态码: $curl_output"
    echo "dotnet nuget 结果: $nuget_result"
    echo "dotnet nuget 输出: $nuget_output"
    
    if [ "$curl_output" == "200" ] && [ $nuget_result -eq 0 ]; then
        echo "✅ 源可用: $url"
    else
        echo "❌ 源不可用: $url"
    fi
    
    # 清理测试源
    dotnet nuget remove source "test_source_$RANDOM" > /dev/null 2>&1
}

# 网络诊断
echo "网络诊断："
echo "DNS 配置："
cat /etc/resolv.conf
echo "---"
echo "网络连接测试："
ping -c 4 8.8.8.8
echo "---"

# 并行测试源
for source in "${sources[@]}"; do
    test_nuget_source "$source" &
done

# 等待所有后台任务完成
wait